package com.asiainfo.dacp.datastash.open.controller;

import com.asiainfo.dacp.datastash.domain.inter.models.DataStashUnitInter;
import com.asiainfo.dacp.datastash.domain.inter.repos.DataStashUnitInterRepository;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by winter on 2018/1/29.
 */
@RestController
@RequestMapping("/open/datastash")
@Api(tags={"datastash-unit-inter"},value="单元操作")
public class DataStashUnitInterController {
    @Autowired
    private DataStashUnitInterRepository dataStashUnitInterRepository;

    @RequestMapping(value = "unit/inter", method = RequestMethod.POST)
    @ApiOperation(tags = {"unitInter"}, value = "创建单元操作")
    public Map stepDefineCreator(@RequestBody String body, HttpServletRequest request, Model model, HttpServletResponse resp)
            throws  Exception {
        String responseBody = null;
        Map responseMessage = new HashMap();
        responseMessage.put("success", true);
        DataStashUnitInter unitInter =null;
        try{
            unitInter = new Gson().fromJson(body,DataStashUnitInter.class);
            dataStashUnitInterRepository.save(unitInter);
            responseMessage.put("inter",unitInter);
        }catch(RuntimeException e){
            e.printStackTrace();
            responseBody = "操作失败！"+e.getMessage();
            responseMessage.put("msg", responseBody);
            responseMessage.put("success", false);
        }
        return responseMessage;
    }

}
